package com.ews.onlineexchange.service.observer;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.service.CandidateSupplierSelector;
import com.ews.onlineexchange.service.DeterminedRecordService;
import com.ews.onlineexchange.service.NotificationService;
import com.ews.onlineexchange.service.SmsService;
import com.ews.onlineexchange.service.SupplierSelector;
import com.ews.onlineexchange.service.TradeSeqNoGenerator;
import com.ews.onlineexchange.service.TradeService;
import com.ews.onlineexchange.service.qualifier.BulletinAuditEvent;
import com.ews.onlineexchange.service.schedule.BulletinAutoPassTimer;
import com.ews.onlineexchange.service.schedule.BulletinOverTimer;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class BulletinAuditObserver {
	
	@Inject
	EntityManager entityManager;
	
	@Inject
	BulletinOverTimer bulletinOverTimer;
	
	@Inject
	BulletinAutoPassTimer bulletinAutoPassTimer;	
	
	@Inject
	SmsService smsService;
	
	@Inject
	CandidateSupplierSelector candidateSupplierSelector;
	
	@Inject
	SupplierSelector supplierSelector;
	
	@Inject
	TradeSeqNoGenerator tradeSeqNoGenerator;	

	@Inject
	DeterminedRecordService recordService;
	
	@Inject
	NotificationService  notificationService;
	
	@Inject
	TradeService tradeService;
	
	public void listenBulletinAudit(@Observes @BulletinAuditEvent Bulletin bulletin) {		
		bulletin = entityManager.find(Bulletin.class, bulletin.getId());
		bulletinOverTimer.registerNewBulletin(bulletin);
		bulletinAutoPassTimer.registerNewBulletin( bulletin );
		selectCandidateSuppliers(bulletin);
		smsService.addInviteMessageToSupplier(bulletin);
	}
	
	public void selectCandidateSuppliers(Bulletin bulletin) {				
		List<ServiceSupplier> supplierList = candidateSupplierSelector.getResultList(bulletin);
		
		List<Trade> conflictingTrades = tradeService.queryExistDealers( bulletin );		
		List<String> existSupplierList = new ArrayList<>();
		for ( Trade trade : conflictingTrades ) {
			existSupplierList.add( trade.getSupplier().getId() );
		}		
		
		for (ServiceSupplier serviceSupplier : supplierList) {
			
			/*
			 * 分数不够的中介，不能接受邀请
			 */
			if ( serviceSupplier.getCreditScore().compareTo( bulletin.getCreditScoreLimit() ) < 0 ) {
				continue;
			}
			
			if ( recordService.isExistSupplierInRecord( serviceSupplier.getId() ) ) {// 当该中介处于整改期间，不给他发送邀请
				notificationService.sendInModificationNotification( bulletin, serviceSupplier );
				continue;
			}
			
			/*
			 * 互斥项目中选的中介，不能接受提醒
			 */
			if ( existSupplierList.contains( serviceSupplier.getId() ) ) {
				continue;
			}
			
			Trade trade = new Trade();
			trade.setSupplier(serviceSupplier);
			trade.setBulletin(bulletin);
			trade.setSerialNumber(tradeSeqNoGenerator.getNextSeqNo());
			entityManager.persist(trade);
			existSupplierList.add(serviceSupplier.getId()); 
		}
	}
	
}
